package com.hurui.xiaoming.activity.finance;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.adapter.FinanceAdapter;
import com.hurui.xiaoming.bean.Li_FinanceResult;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.DefultData;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FinanceActivity extends BaseActivity {
    private ImageView back_iv ;
    private XRecyclerView finance_rv ;
    private FinanceAdapter adapter ;
    private List<Object> lists;
    private Map<String,String> map;

    private static int INIT_PAGE = 1;
    int page = INIT_PAGE;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finance);
        tintManager.setStatusBarTintColor(R.color.systembartintColor2);
        lists = new ArrayList<Object>();
        map=new HashMap<>();
        initView();
        initAdapter();
        initAction();

    }

    private void initAdapter() {
        adapter = new FinanceAdapter(this  );
        finance_rv.setLayoutManager(new LinearLayoutManager(this));
        finance_rv.setAdapter(adapter);

    //   adapter.notifyData(`);
    }

    @Override
    public void initView() {

        back_iv = (ImageView) findViewById(R.id.back_iv);
        finance_rv = (XRecyclerView) findViewById(R.id.finance_rv);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getIntentData(page);
    }

    @Override
    public void initAction() {
        finance_rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                getIntentData(INIT_PAGE);

            }

            @Override
            public void onLoadMore() {
                page++;
                getIntentData(page);
            }
        });

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WriteReadSharedPrefs.outUser(mContext);
                finish();
            }
        });

        adapter.setUpdateView(new FinanceAdapter.UpdateView() {
            @Override
            public void updateView() {
                getIntentData(page);
            }
        });
    }

    /**
     * 获取网络数据
     */
   /* public void getData(){
        map.put(PamaryKeys.UI_ID, ""+ WriteReadSharedPrefs.readUserID(mContext));
        // map.put(PamaryKeys.UI_ID, "1");
        map.put("oi_state", "2");
        apiSevice.onHTttp_myOrder(map).enqueue(new Callback<Li_MyList_Result>() {
            @Override
            public void onResponse(Call<Li_MyList_Result> call, Response<Li_MyList_Result> response) {
                Li_MyList_Result body = response.body();
                List<Li_MyList_Result.DataBean> data = body.getData();
                adapter.notifyData(data);
                finance_rv.refreshComplete();
                finance_rv.loadMoreComplete();
//                Toast.makeText(getActivity(),"请求成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<Li_MyList_Result> call, Throwable t) {
                finance_rv.refreshComplete();
                finance_rv.loadMoreComplete();
            }
        });
    }*/


    //获取网络数据
    public void getIntentData(int page){
        map.put(PamaryKeys.UI_ID, ""+ WriteReadSharedPrefs.readUserID(mContext));
        // map.put(PamaryKeys.UI_ID, "1");
        map.put("ui_type", "2");
        map.put("page_num",String.valueOf(page));
        Call<Li_FinanceResult> call = apiSevice.onHttpPostFinanceData(DefultData.indexData,map);
        call.enqueue(new Callback<Li_FinanceResult>() {
            @Override
            public void onResponse(Call<Li_FinanceResult> call, Response<Li_FinanceResult> response) {
                if(response.isSuccessful()){
                    Li_FinanceResult li_financeResult = response.body();

                    List<Li_FinanceResult.DataBean> data = li_financeResult.getData();
                    if(data.size() > 0){
                        adapter.notifyData(data);
                    }
                    finance_rv.refreshComplete();
                    finance_rv.loadMoreComplete();
//                Toast.makeText(getActivity(),"请求成功",Toast.LENGTH_SHORT).show();

                }else{
                    onToast("失败");
                }
            }

            @Override
            public void onFailure(Call<Li_FinanceResult> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
                finance_rv.refreshComplete();
                finance_rv.loadMoreComplete();
            }
        });
    }

}
